package com.hd.trans.framework.dialog

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Parcelable
import android.view.*
import androidx.annotation.DrawableRes
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseQuickAdapter.AnimationType
import com.hd.trans.R
import com.hd.trans.databinding.DialogRecordMenuBinding
import com.hd.trans.db.bean.FileTranslateRecord
import com.hd.trans.ui.adapter.RecordMenuAdapter
import kotlinx.android.parcel.Parcelize

class RecordMenuDialog: DialogFragment() {

    private lateinit var adapter: RecordMenuAdapter
    lateinit var mDataBinding: DialogRecordMenuBinding
    private var onItemClickListener: ServiceObjectListener? = null
    private var fileTranslateRecord: FileTranslateRecord? = null
    private var recordPosition: Int = 0

    fun setOnItemClickListener(listener: ServiceObjectListener): RecordMenuDialog{
        onItemClickListener = listener
        return this
    }

    fun setServiceObject(record: FileTranslateRecord, position: Int): RecordMenuDialog{
        fileTranslateRecord= record
        recordPosition = position
        return this
    }

    private val list by lazy {
        arrayListOf(
            Worker(R.mipmap.icon_trans_share,"分享", WorkerType.share),
            Worker(R.mipmap.icon_trans_edit_file,"重命名", WorkerType.rename),
            Worker(R.mipmap.icon_trans_delete_file,"删除", WorkerType.delete),
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //设置dialog样式
        setStyle(STYLE_NO_TITLE, R.style.BottomDialogStyle)
    }

    override fun onStart() {
        super.onStart()
        //可以在createView里一次写
        //设置dialog 宽高
        //val width = Resources.getSystem().displayMetrics.widthPixels
        dialog?.window!!.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        dialog?.window!!.setGravity(Gravity.BOTTOM)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = LayoutInflater.from(context)
            .inflate(R.layout.dialog_record_menu, container, false)
        mDataBinding = DialogRecordMenuBinding.bind(view)
        initView()
        initDialog()
        setListener()
        return view
    }

    private fun initView() {
        mDataBinding.apply {

            recyclerView.layoutManager = LinearLayoutManager(context)
            recyclerView.itemAnimator = DefaultItemAnimator()
            adapter = RecordMenuAdapter( list)
            //设置条目加载动画。
            adapter.animationEnable = true
            adapter.setAnimationWithDefault(AnimationType.SlideInBottom)
            recyclerView.adapter = adapter
        }
    }

    private fun initDialog() {
        dialog?.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT)) //设置背景为透明
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
    }

    private fun setListener() {
        adapter.setOnItemClickListener { adapter, view, position ->
            onItemClickListener?.onServiceObjectListener(adapter, view, position,fileTranslateRecord, recordPosition)
            dismiss()
        }

        mDataBinding.tvCancel.setOnClickListener {
            dismiss()
        }
    }
}

interface ServiceObjectListener{
    fun onServiceObjectListener(adapter:BaseQuickAdapter<*, *>, view: View, position: Int, record: FileTranslateRecord?, recordPosition: Int)
}

@Parcelize
data class Worker(
    @DrawableRes val headRes:Int,
    val name:String,
    val type:WorkerType
): Parcelable

enum class WorkerType{
    share,delete,rename
}